window.onload = function () {
    var storedFontFamily = localStorage.getItem("selectedFont");
    if (storedFontFamily) {
        var bodyElement = document.body;
        bodyElement.style.fontFamily = storedFontFamily;
    }
    var storedReaderBack = localStorage.getItem("selectedReaderBack");
    var storedContentBack = localStorage.getItem("selectedContentBack");
    var storedFontColor = localStorage.getItem("selectedfontColor");
    var storedChangeBoxBack = localStorage.getItem("selectedChangeBoxBack");
    var storedChangeButtonBack = localStorage.getItem("selectedChangeButtonBack");
    var storedChangeButtonFontColor = localStorage.getItem("selectedChangeButtonFontColor");
    if (storedReaderBack) {
        var readerElement = document.querySelector('.reader');
        readerElement.style.backgroundColor = storedReaderBack;
    }
    if (storedContentBack) {
        var contentElement = document.querySelector('.content');
        contentElement.style.backgroundColor = storedContentBack;
    }
    if (storedFontColor) {
        var bodyElement = document.body;
        bodyElement.style.color = storedFontColor;
    }
    if (storedChangeBoxBack) {
        var changeBoxs = document.querySelectorAll('.change-box');
        for (let i = 0; i < changeBoxs.length; i++) {
            changeBoxs[i].style.backgroundColor = storedChangeBoxBack;
        }
    }
    if (storedChangeButtonBack && storedChangeButtonFontColor) {
        var changeButtons = document.querySelector('.change').querySelectorAll('button');
        for (let i = 0; i < changeButtons.length; i++) {
            changeButtons[i].style.backgroundColor = storedChangeButtonBack;
            changeButtons[i].style.color = storedChangeButtonFontColor;
        }
    }
};

function changePreviousChapter() {
    // 获取当前章节
    var url = window.location.pathname;
    // console.log(url);
    var pathSegments = url.split("/");
    var book_id = pathSegments[2];
    var chapter_num = parseInt(pathSegments[3]) - 1;
    var chapter_id = chapter_num.toString();
    // console.log(book_id);
    // console.log(chapter_id);
    var read_url = "/read/" + book_id;
    read_url += "/";
    read_url += chapter_id;
    // console.log(read_url);
    // window.history.pushState({}, "", read_url);
    if (chapter_num === 0) {
        alert("已经是第一章啦");
    } else {
        window.location.assign(read_url);
    }
}
function changeNextChapter() {
    // 获取当前章节
    var url = window.location.pathname;
    // console.log(url);
    var pathSegments = url.split("/");
    var book_id = pathSegments[2];
    var chapter_num = parseInt(pathSegments[3]) + 1;
    var chapter_id = chapter_num.toString();
    // console.log(book_id);
    // console.log(chapter_id);
    var read_url = "/read/" + book_id;
    read_url += "/";
    read_url += chapter_id;
    // console.log(read_url);
    // window.history.pushState({}, "", read_url);
    window.location.assign(read_url);
}

function changeColor() {
    var readerElement = document.querySelector('.reader');
    var contentElement = document.querySelector('.content');
    var bodyElement = document.body;
    var changeBoxs = document.querySelectorAll('.change-box');
    var changeButtons = document.querySelector('.change').querySelectorAll('button');
    if (window.getComputedStyle(readerElement).backgroundColor === 'rgb(231, 226, 215)') {
        readerElement.style.backgroundColor = 'rgb(10, 10, 10)';
        contentElement.style.backgroundColor = 'rgb(17, 17, 17)';
        bodyElement.style.color = 'rgb(161, 160, 157)';
        for (let i = 0; i < changeBoxs.length; i++) {
            changeBoxs[i].style.backgroundColor = 'rgb(17, 17, 17)';
        }
        for (let i = 0; i < changeButtons.length; i++) {
            changeButtons[i].style.backgroundColor = 'rgb(10, 10, 10)';
            changeButtons[i].style.color = 'rgb(161, 160, 157)';
        }
    } else {
        readerElement.style.backgroundColor = 'rgb(231, 226, 215)';
        contentElement.style.backgroundColor = 'rgb(243, 239, 230)';
        bodyElement.style.color = 'rgb(0, 0, 0)';
        for (let i = 0; i < changeBoxs.length; i++) {
            changeBoxs[i].style.backgroundColor = 'rgb(243, 239, 230)';
        }
        for (let i = 0; i < changeButtons.length; i++) {
            changeButtons[i].style.backgroundColor = 'rgb(231, 226, 215)';
            changeButtons[i].style.color = 'rgb(0, 0, 0)';
        }
    }
    localStorage.setItem("selectedReaderBack", readerElement.style.backgroundColor);
    localStorage.setItem("selectedContentBack", contentElement.style.backgroundColor);
    localStorage.setItem("selectedfontColor", bodyElement.style.color);
    localStorage.setItem("selectedChangeBoxBack", changeBoxs[0].style.backgroundColor);
    localStorage.setItem("selectedChangeButtonBack", changeButtons[0].style.backgroundColor);
    localStorage.setItem("selectedChangeButtonFontColor", changeButtons[0].style.color);
}

function changeFont(font_family) {

    var bodyElement = document.body;
    bodyElement.style.fontFamily = font_family;

    localStorage.setItem("selectedFont", bodyElement.style.fontFamily);
}